﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Windows.Forms;
using HRM.BusinessLogic;

namespace HRM
{
    public partial class frmDepartmentList3 : FormBase
    {
        DataTable _dtData = null;
        int _masterId = 0;
        int _branchId = 0;
        public override void Show_Form()
        {
            Show();
        }


        public frmDepartmentList3()
        {
            InitializeComponent();

            Init_Grid();
            UISetting.LoadTreeViewHRMProfile(ref tvDepartment);
            tvDepartment.ExpandAll();
            //
            _branchId = Program.intBranchID;
            loadList();
            KeyPreview = true;
        }
        //Khởi tạo danh sách cho grid
        DataGridHandle _mGridHandle;
        protected void Init_Grid()
        {
            var arrColumn = new List<DataGridViewColumn>
                                {
                                    FormGlobals.Grid_NewColumn<DataGridViewTextBoxColumn>("STT", "RN", 30, "", true),
                                    FormGlobals.Grid_NewColumn<DataGridViewTextBoxColumn>("Mã phòng ban","Department_Code", 100),
                                    FormGlobals.Grid_NewColumn<DataGridViewTextBoxColumn>("Tên phòng ban","Department_Name", 300),
                                    FormGlobals.Grid_NewColumn<DataGridViewTextBoxColumn>("Department_ID", "Department_ID", 0),
                                    FormGlobals.Grid_NewColumn<DataGridViewTextBoxColumn>("Department_BranchID", "Department_BranchID", 0),
                                    FormGlobals.Grid_NewColumn<DataGridViewTextBoxColumn>("Department_MasterID", "Department_MasterID", 0),
                                };

            //
            _mGridHandle = FormGlobals.Grid_Init(dgvList, arrColumn, "", 1);
            _mGridHandle.Row_Validated += m_GridHandle_Row_Validated;
        }

        void m_GridHandle_Row_Validated(DataGridViewRow row)
        {
            row.Cells["RN"].Value = row.Index + 1;
        }

        private void loadList()
        {
            try
            {
                _dtData = Department_Controller.Instance().Department_GetByMasterID(_branchId, _masterId).Tables[0];
                _dtData.Columns["Department_BranchID"].DefaultValue = Program.intBranchID;
                _dtData.Columns["Department_MasterID"].DefaultValue = _masterId;
                Globals.DataTable_RowNumber(_dtData);

                try
                {
                    Globals.DataTable_SetUnique(_dtData, "Department_BranchID", "Department_Code", "Department_Name");
                }
                catch (Exception ex)
                {
                    FormGlobals.Message_Warning("Có sự trùng lặp");
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }

            //
            dgvList.DataSource = _dtData;
        }


        private void frmDepartmentList3_KeyDown(object sender, KeyEventArgs e)
        {
            switch (e.KeyCode)
            {
                case Keys.Escape:
                    Dispose();
                    Close();
                    break;

            }
        }


        //Lưu dữ liệu
        private void btnSave_Click(object sender, EventArgs e)
        {
            // dtData.AcceptChanges();
            if (FormGlobals.Control_CheckError(dgvList))
                FormGlobals.App_ProgressShow(delegate()
                {
                    Department_Controller.Instance().InsertTable(_dtData);

                    //Load lại danh sách
                    ReloadDepartmentList();

                    loadList();

                }, "Lưu dữ liệu", "Lưu thành công");
           
        }

        private void tvDepartment_AfterSelect(object sender, TreeViewEventArgs e)
        {
            if (e.Node.Level == 1)
            {
                _branchId = Convert.ToInt32(e.Node.Tag);
                _masterId = 0;
            }
            else if (e.Node.Level == 2)
            {
                _branchId = Convert.ToInt32(e.Node.Parent.Tag);
                _masterId = Convert.ToInt32(e.Node.Tag);
            }
            else if (e.Node.Level > 2)
            {
                _masterId = Convert.ToInt32(e.Node.Parent.Tag);
                _branchId = Convert.ToInt32(GetBranchNode(e.Node).Tag);
            }
            loadList();
        }
        TreeNode GetBranchNode(TreeNode node)
        {
            var retVal = node;
            if (node.Level >= 2)
                retVal = node.Parent;
            return retVal;
        }
        public void ReloadDepartmentList()
        {
            UISetting.LoadTreeViewHRMProfile(ref tvDepartment);
            tvDepartment.ExpandAll();
        }
    }
}


